var datasource = "cpmis_ds";

/**
 * 更新工作量（预算回退使用）
 * @param type：更新类型（来自flow.util）
 * @param v：更新的值，预算来源修改的值
 * @param bt：立项对应的预算来源，也就是要更新的预算
 */
function updateWorlloadHasValue(type, v, bt) {
    var yyl = "";
    var kyl = "";
    var djl = "";
    println("【调整工作量】--------------- 开始 ---------------");
    println("1. 调整类型：" + type);
    println("2. 调整的工作量：" + v);
    if (WorkLoad_Type.type4.equals(type)) {
        kyl = "+" + v;
        djl = "-" + v;
    } else if (WorkLoad_Type.type5.equals(type)) {
        yyl = "-" + v;
        kyl = "+" + v;
    } else if (WorkLoad_Type.type2.equals(type)) {
        djl = "+" + v;
        yyl = "-" + v;
    } else if (WorkLoad_Type.type6.equals(type)) {
        djl = "-" + v;
        yyl = "+" + v;
    }
    var sql = "update tlk_budget_management set item_kyl = item_kyl" + kyl + ", item_djl = item_djl" + djl + ", item_yyl = item_yyl" + yyl + " where item_bt = '" + bt + "'"; //ysly
    println("3. 调整工作量的SQL：" + sql);
    updateByDSName(datasource, sql);
    println("【调整工作量】--------------- 结束 ---------------");
}


/**
 * 更新工作量数据（电子需求单使用）
 * 用于在操作一个具体立项流程的时候
 */
function updateWorkload(type) {
    var doc = getCurrentDocument();
    if (isNotNull(type)) {
        var domainid = getDomainid();
        println("【开始调整工作量】------------ START ------------");
        println("调整类型：" + type);
        //var sql_1 = "select sum(item_zhgzl_zs) as item_total, '" + domainid + "'  as domainid from tlk_workload_assess_info where parent = '" + doc.getId() + "'";
        //现在添加构件采购模块，因此工作量需要添加构件采购支出
        var sql_1 = "select (item_total1 + item_total2) as item_sum, '" + domainid + "'  as domainid from ( " +
            " (SELECT  " +
            " ifnull(SUM(item_zhgzl_zs), 0) AS item_total1 " +
            " FROM " +
            " tlk_workload_assess_info " +
            " WHERE " +
            " parent = '" + doc.getId() + "') as s1,  " +
            " (SELECT  " +
            " ifnull(sum(ITEM_ZSRY), 0) as item_total2 " +
            " FROM " +
            " tlk_Component_Purchase_List " +
            " WHERE " +
            " parent ='" + doc.getId() + "') as s2 )";
        println("1.1. 获取调整量SQL ：" + sql_1);
        var result = findBySQL(sql_1);
        var v = result.getItemValueAsDouble("sum");
        println("1.2. 获取调整量 : " + v);
        //var v = round(v, 10); //可能影响老数据，因此取消
        println("1.3. 不进行获四舍五入后的调整量 : " + v);
        //预算管理信息
        var ysgl = getItemValueAsString();
        var sql_2 = "select bm.id as item_id, bm.item_kyl as item_kyl, bm.item_djl as item_djl, bm.item_yyl as item_yyl, '" + domainid + "'  as domainid  " +
            "from tlk_budget_management bm " +
            "left join tlk_setuo_carryout st " +
            "on bm.ITEM_BT = st.item_ysly " +
            "where st.id = '" + doc.getId() + "'";
        var result = findBySQL(sql_2);
        println("2.1. 对应预算信息SQL：" + sql_2);
        if (isNotNull(result)) {
            var id = result.getItemValueAsString("id");
            var kyl = result.getItemValueAsDouble("kyl");
            var djl = result.getItemValueAsDouble("djl");
            var yyl = result.getItemValueAsDouble("yyl");
            println("2.2. 四个变量：kyl:" + kyl + "；djl:" + djl + "；yyl:" + yyl);
            if (WorkLoad_Type.type1.equals(type)) { //增加冻结量，减少可用量
                djl = djl + v;
                kyl = kyl - v;
            } else if (WorkLoad_Type.type2.equals(type)) { //增加已用量，减少冻结量
                djl = djl - v;
                yyl = yyl + v;
            } else if (WorkLoad_Type.type3.equals(type)) { //增加冻结量，减少已用量
                djl = djl + v;
                yyl = yyl - v;
            } else if (WorkLoad_Type.type4.equals(type)) { //增加可用量， 減少冻结量
                kyl = kyl + v;
                djl = djl - v;
            } else if (WorkLoad_Type.type5.equals(type)) {
                yyl = yyl - v;
                kyl = kyl + v;
            }
            var sql_3 = "update tlk_budget_management set item_kyl = " + kyl + ", item_djl = " + djl + ", item_yyl = " + yyl + " where id = '" + id + "'";
            println("3. 调整工作量：" + sql_3);
            updateByDSName(datasource, sql_3);
            println("【开始调整工作量】------------ END ------------");
        }
    }
}